const express=require('express')
const router =express.Router()
const db=require('../../utils/db')
// 右侧表格数据
router.post('/rightTable',(req,res)=>{
    const {mName,mType,page,currentPage}=req.body;
    let select
    if(mType===2){
     select =`select m_name,m_id ,m_type,m_spec,m_price from  medicinal_information where m_name like '%${mName}%' limit ${(currentPage- 1) * page},${page};`
    }else{
      select =`select m_name,m_id ,m_type,m_spec,m_price from  medicinal_information where m_type=${mType} and m_name like '%${mName}%' limit ${(currentPage- 1) * page},${page};`
    }
   
    let select2=`select count(*) as total from medicinal_information where m_type=${mType} and m_name like '%${mName}%' limit ${(currentPage- 1) * page},${page};`
    db.query(select,(err,data)=>{
        if(err){
            res.send({ err: error, msg: 'error' })
        }else{
            db.query(select2,(err2,data2)=>{
                if(err2){
                    res.send({ err2: error, msg: 'error' }) 
                }else{
                    res.send({ data: data, msg: "success", total: data2[0].total })
                }
            })
        }
    })

})
// 附加费用
router.post('/expense',(req,res)=>{
    let select=`select p_id,p_name,p_person,p_price from  price_setting`
    db.query(select,(err,data)=>{
        if(err){
            res.send({ err: error, msg: 'error' })
        }else{
            res.send({data:data,msg: 'success'})
        }
    })
})
// 所有患者姓名搜索
router.post('/patientSel',(req,res)=>{
    let select =`select * from patient `
    db.query(select,(err,data)=>{
        if(err){
            res.send({err:error,msg:'error'})
        }else{
            res.send({data:data,msg:'success'})
        }
    })
})
// 个人患者
router.post('/personSel',(req,res)=>{
    const {sName}=req.body
    let select=`select *from patient where s_name='${sName}'`
    db.query(select,(err,data)=>{
        if(err){
            res.send({err,msg:'error'})
        }else{
            res.send({data:data,msg:'success'})
        }
    })
})
// 根据患者和医生id查询
router.post('/searchNew',(req,res)=>{
    const {reg_s_id,reg_user_id}=req.body
    let select=`SELECT reg_id FROM register_record  WHERE reg_s_id = ${reg_s_id} AND reg_user_id=${reg_user_id}`
    db.query(select,(err,data)=>{
        if(err){
            res.send({err,msg:'error'})
        }else{
            res.send({data:data,msg:'success'})
        }
    })
})
// 病例查询
router.post('/lookBingLi',(req,res)=>{
    const {h_u_id}=req.body
    let select=`SELECT * FROM history_case WHERE h_u_id=${h_u_id}`
    db.query(select,(err,data)=>{
        if(err){
            res.send({err,msg:'error'})
        }else{
            res.send({data:data,msg:'success'})
        }
    })
})
module.exports=router